package com.ntschy.web.controller.sys;

import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.core.entity.dataobject.SysMenu;
import com.ntschy.sys.entity.dto.menu.SysMenuAddUpdateDTO;
import com.ntschy.sys.entity.dto.menu.SysMenuPageDTO;
import com.ntschy.sys.entity.mapstruct.SysMenuMapstruct;
import com.ntschy.sys.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author ezreal_geng
 */
@RestController
@RequestMapping("/sys/menu")
@Api(tags = "菜单接口")
@Validated
public class SysMenuController extends BaseController<SysMenu> {

    @Autowired
    private ISysMenuService menuService;

    // * ================================================基础增删改查BEGIN============================================= *
    @ApiOperation(value = "查询菜单列表", notes = "")
    @ApiImplicitParam(name = "queryDto", value = "菜单列表查询实体类", required = true, dataType = "SysMenuPageDTO")
    //@PreAuthorize("@ss.hasPermi('sys:menu:page')")
    @PostMapping("/pageMenu")
    public AjaxResult<List<SysMenu>> pageMenu(@RequestBody SysMenuPageDTO pageDTO) {
        SysMenu queryMenu = SysMenuMapstruct.SYS_MENU_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<SysMenu> list = menuService.pageMenu(queryMenu);
        return AjaxResult.success("菜单列表", list);
    }

    @ApiOperation(value = "根据菜单ID获取菜单详情", notes = "")
    @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:menu:detail')")
    @GetMapping("/fetchMenu")
    public AjaxResult<SysMenu> fetchMenu(@NotBlank(message = "菜单ID不能为空") @Length(min = 36, max = 36, message = "菜单ID不合法") String menuId) {
        return AjaxResult.success("获取菜单详情成功", menuService.fetchMenu(menuId));
    }

    @ApiOperation(value = "新增菜单", notes = "")
    @ApiImplicitParam(name = "addDto", value = "新增菜单实体类", required = true, dataType = "SysMenuAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:menu:add')")
    @PostMapping("/String")
    public AjaxResult<String> addMenu(@RequestBody SysMenuAddUpdateDTO addDto) {
        if(menuService.checkMenuNameExist(addDto.getName())) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "菜单名称已存在");
        } else if (addDto.getId().equals(addDto.getParentId())) {
            return AjaxResult.error("修改菜单'" + addDto.getName() + "'失败，上级菜单不能选择自己");
        }
        SysMenu addMenu = SysMenuMapstruct.SYS_MENU_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(menuService.addMenu(addMenu), "新增菜单成功", "新增菜单失败");
    }

    @ApiOperation(value = "更新菜单", notes = "")
    @ApiImplicitParam(name = "updateDto", value = "更新菜单实体类", required = true, dataType = "SysMenuAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:menu:update')")
    @PostMapping("/updateMenu")
    public AjaxResult<String> updateMenu(@RequestBody SysMenuAddUpdateDTO updateDto) {
        if(menuService.checkMenuNameExist(updateDto.getName())) {
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "菜单名称已存在");
        } else if (updateDto.getId().equals(updateDto.getParentId())) {
            return AjaxResult.error("修改菜单'" + updateDto.getName() + "'失败，上级菜单不能选择自己");
        }
        SysMenu updateMenu = SysMenuMapstruct.SYS_MENU_MAP_STRUCT.addUpdateDtoToDo(updateDto);
        return toAjax(menuService.updateMenu(updateMenu), "更新菜单成功", "更新菜单失败");
    }

    @ApiOperation(value = "根据菜单ID删除菜单", notes = "")
    @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:menu:detail')")
    @GetMapping("/deleteMenu")
    public AjaxResult<String> deleteMenu(@NotBlank(message = "菜单ID不能为空") @Length(min = 36, max = 36, message = "菜单ID不合法") String menuId) {
        if(menuService.checkHasChildMenu(menuId)) {
            return AjaxResult.error(HttpStatus.ERROR, "存在子菜单,不允许删除");
        } else if(menuService.checkMenuExistRole(menuId)) {
            return AjaxResult.error(HttpStatus.ERROR, "菜单已分配,不允许删除");
        }
        return toAjax(menuService.deleteMenu(menuId), "删除菜单成功", "删除菜单失败");
    }
    // * =================================================基础增删改查END============================================== *

}
